<?php
/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2018/5/22
 * Time: 下午2:27
 */

namespace common\service;

use common\models\Activity;
use yii\mongodb\Database;
use yii;

/**
 * @SuppressWarnings(PHPMD)
 */
class ActivityService
{
    /**
     * 获取当前进行的活动
     * @param $channel
     * @return null
     */
    public function getCurrentActivity($symbol)
    {
        $time = time();
        $activityCategory = ServiceFactory::getInstance()->getActivityCategoryService()->getCategoryBySymbol($symbol);
        if (empty($activityCategory)) {
            throw new yii\web\NotFoundHttpException('标识不存在');
        }
        $coupon_catalog = intval($activityCategory['id']);

        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase("web");
        $collection = $database->getCollection("activity");
        $activityList = $collection->find(['activity_catalog' => intval($coupon_catalog)], [], ['sort' => ['start_time' => 1]])->toArray();
        $currentActivity = null;
        if (count($activityList)) {
            foreach ($activityList as $key => $activity) {
                if (($activity['start_time']) > $time) {
                    $currentActivity = $activityList[$key];
                    $currentActivity['status'] = Activity::STATUS_NOT_START;
                    break;
                }
                if ($activity['start_time'] < $time && $activity['end_time'] > $time) {
                    $currentActivity = $activityList[$key];
                    $currentActivity['status'] = Activity::STATUS_ON_GOING;
                    break;
                }
                if ($activity['end_time'] < $time) {
                    $currentActivity = $activityList[$key];
                    $currentActivity['status'] = Activity::STATUS_NO_REMAIN;
                    break;
                }
            }
        }
        return $currentActivity;
    }

    public function getActivityList($symbol)
    {
        $time = time();
        $activityCategory = ServiceFactory::getInstance()->getActivityCategoryService()->getCategoryBySymbol($symbol);
        if (empty($activityCategory)) {
            throw new yii\web\NotFoundHttpException('标识不存在');
        }
        $activity_catalog = intval($activityCategory['id']);

        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase("web");
        $collection = $database->getCollection("activity");
        $currentActivityList = $collection->find(['activity_catalog' => intval($activity_catalog)], ['coupon_catalog', 'id', 'start_time', 'end_time', 'name', 'scene'], ['sort' => ['start_time' => 1]])->toArray();
        foreach ($currentActivityList as $key => $activity) {
            if ($activity['start_time'] > $time || $activity['end_time'] < $time) {
                unset($currentActivityList[$key]);
                continue;
            }
        }
        return $currentActivityList;
    }

    public function getActivityListNew($symbol)
    {
        $time = time();
        $activityCategory = ServiceFactory::getInstance()->getActivityCategoryService()->getCategoryBySymbol($symbol);
        if (empty($activityCategory)) {
            throw new yii\web\NotFoundHttpException('标识不存在');
        }
        $activity_catalog = intval($activityCategory['id']);

        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase("web");
        $collection = $database->getCollection("activity");
        $activityList = $collection->find(['activity_catalog' => intval($activity_catalog)], ['coupon_catalog', 'id', 'start_time', 'end_time', 'name', 'scene'], ['sort' => ['start_time' => 1]])->toArray();
        return $activityList;
    }
}
